package com.yzy.controller;

import com.yzy.dao.BookDao;
import com.yzy.utils.Utils;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PatientBookButtonServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext application = request.getServletContext();
        String pphone = (String) application.getAttribute("PlonginPhone1");
        String booktime = (String) application.getAttribute("booktime");
        Integer doctorId;
        BookDao dao = new BookDao();
        int result=0;
        PrintWriter out=null;
        //1.调用请求对象
        doctorId = Integer.valueOf(request.getParameter("doctorId"));
        //2
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
        Date nowDate= new Date();
        String SS= sdf.format(nowDate);
        result = dao.addbook(pphone,doctorId,SS);
        //3
        response.setContentType("text/html;charset=utf-8");
        out=response.getWriter();
        if(result ==1){
            //放booktime进表
            Utils util1 = new Utils();
            String sql1 = "update book set booktime=? where pphone=?";
            PreparedStatement ps1=util1.createStatement(sql1);
            try {
                ps1.setString(1,booktime);
                ps1.setString(2,pphone);

                ps1.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                util1.close();
            }
            out.print("<font style='color:red;font-size:40'>医生预约成功</font>");
        }else{
            out.print("<font style='color:red;font-size:40'>医生预约失败</font>");
        }
    }
}
